package com.aimuceliang.aimuceliang.utils;

import java.util.HashMap;

/**
 * Created by zhangjing on 2017/10/31.
 */

public class wochengDataAnalysisUtil {

    // 02 34 30 20 20 20 20 31 30 32 20 20 20 30 30 30 20 20 20 31 30 34 20 20 31 0D
    // 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    static String strEarNum      = null;  // 耳标
    static String strNumber      = null;  // 称重数量
    static String strWeightTotal = null;  // 总重量
    static String strWeightMin   = null;  // 最轻
    static String strWeightMax   = null;  // 最重
    static String strWeightAvg   = null;  // 平均重量
    static String strWeightTare  = null;  // 皮重
    static String strWeightCurr  = null;  // 当前物品重量
    static int    iNumber        = 0;     // 称重物品数量
    static int    iDecimalPlaces = 1;     // 小数点位置

    static double dWeightMin = 0.0;
    static double dWeightMax = 0.0;

    void wochengDataAnalysisUtil() {
        initResData();
    }

    private static void initResData() {
        strNumber      = null;
        strWeightTotal = null;
        strWeightMin   = null;
        strEarNum      = null;
        strWeightMax   = null;
        strWeightAvg   = null;
        strWeightTare  = null;
        strWeightCurr  = null;
        iNumber        = 0;
        iDecimalPlaces = 1;
        dWeightMin     = 0.0;
        dWeightMax     = 0.0;
    }

    private static int bytes2Int(byte[] data, int start, int len) {
        int sum = 0;
        int end = start + len;
        for (int i = start; i < end; i++) {
            int n = ((int)data[i]) & 0xff;
            n <<= (--len) * 8;
            sum += n;
        }
        return sum;
    }

    private static int bytes2Int(byte data) {
        return (int)data;
    }

    private static String int2FullBinaryString(int num) {
        char[] chs = new char[Integer.SIZE];
        for(int i = 0; i < Integer.SIZE; i++) {
            chs[Integer.SIZE - 1 - i] = (char)(((num >> i) & 1) + '0');
        }
        return new String(chs);
    }

    private static int getDecimalPlaces(byte[] data, int start, int len) {
        String BinaryString = int2FullBinaryString(Integer.parseInt(String.valueOf(bytes2Int(data, start, len))));
        BinaryString = BinaryString.substring(BinaryString.length()-3, BinaryString.length());

        if(BinaryString.equals("001")) {
            return 1;
        } else if(BinaryString.equals("010")) {
            return 1;
        } else if(BinaryString.equals("011")) {
            return 10;
        } else if(BinaryString.equals("100")) {
            return 100;
        } else if(BinaryString.equals("101")) {
            return 1000;
        } else if(BinaryString.equals("110")) {
            return 10000;
        }

        return 1;
    }

    private static double getWeightTotal(byte[] data, int start, int len) {

        int num = 0;
        if((int)data[start+0] - 48 >= 0) {
            num = num + ((int)data[start+0] - 48) * 100000;
        }

        if((int)data[start+1] - 48 >= 0) {
            num = num + ((int)data[start+1] - 48) * 10000;
        }

        if((int)data[start+2] - 48 >= 0) {
            num = num + ((int)data[start+2] - 48) * 1000;
        }

        if((int)data[start+3] - 48 >= 0) {
            num = num + ((int)data[start+3] - 48) * 100;
        }

        if((int)data[start+4] - 48 >= 0) {
            num = num + ((int)data[start+4] - 48) * 10;
        }

        if((int)data[start+5] - 48 >= 0) {
            num = num + ((int)data[start+5] - 48) * 1;
        }

        return (double)num / 100;
    }

    private static double getWeightCurr(byte[] data, int start, int len) {

        int num = 0;
        if((int)data[start+0] - 48 >= 0) {
            num = num + ((int)data[start+0] - 48) * 100000;
        }

        if((int)data[start+1] - 48 >= 0) {
            num = num + ((int)data[start+1] - 48) * 10000;
        }

        if((int)data[start+2] - 48 >= 0) {
            num = num + ((int)data[start+2] - 48) * 1000;
        }

        if((int)data[start+3] - 48 >= 0) {
            num = num + ((int)data[start+3] - 48) * 100;
        }

        if((int)data[start+4] - 48 >= 0) {
            num = num + ((int)data[start+4] - 48) * 10;
        }

        if((int)data[start+5] - 48 >= 0) {
            num = num + ((int)data[start+5] - 48) * 1;
        }

        return (double)num / 100;
    }


    private static double getWeightTare(byte[] data, int start, int len) {

        int num = 0;
        if((int)data[start+0] - 48 >= 0) {
            num = num + ((int)data[start+0] - 48) * 100000;
        }

        if((int)data[start+1] - 48 >= 0) {
            num = num + ((int)data[start+1] - 48) * 10000;
        }

        if((int)data[start+2] - 48 >= 0) {
            num = num + ((int)data[start+2] - 48) * 1000;
        }

        if((int)data[start+3] - 48 >= 0) {
            num = num + ((int)data[start+3] - 48) * 100;
        }

        if((int)data[start+4] - 48 >= 0) {
            num = num + ((int)data[start+4] - 48) * 10;
        }

        if((int)data[start+5] - 48 >= 0) {
            num = num + ((int)data[start+5] - 48) * 1;
        }

        return (double)num / 100;
    }

    private static int getNumber(byte[] data, int start, int len) {
        return (int)data[start] - 48;
    }

    static boolean ansyBlueData(byte[] data) {
        initResData();

        if(data.length != 26) {
            return false;
        }

        iNumber = getNumber(data, 24, 1);
        if(0 == iNumber) {
            strNumber = "0";
            strWeightTotal = "0";
            strWeightMax  = "0";
            strWeightMin  = "0";
            strWeightAvg  = "0";
            return true;
        }

        iDecimalPlaces = getDecimalPlaces(data, 1, 1);
        double dWeightTotal = getWeightTotal(data, 4, 6);
        double dWeightTare  = getWeightTare(data, 10, 6);
        double dWeightCurr  = getWeightCurr(data, 16, 6);

        if(0 == dWeightMax) {
            dWeightMax = dWeightCurr;
        } else {
            int retval = Double.compare(dWeightMax, dWeightCurr);
            if(retval < 0) {
                dWeightMax = dWeightCurr;
            }
        }

        if(0 == dWeightMin) {
            dWeightMin = dWeightCurr;
        } else {
            int retval = Double.compare(dWeightMin, dWeightCurr);
            if(retval > 0) {
                dWeightMin = dWeightCurr;
            }
        }

        if((dWeightTotal < 0) || (dWeightMax < 0) || (dWeightMin < 0) || (iNumber < 0)) {
            return false;
        }

        strNumber = String.valueOf(iNumber);
        strWeightTotal = String.format("%.2f", dWeightTotal);
        strWeightMax = String.format("%.2f", dWeightMax);
        strWeightMin = String.format("%.2f", dWeightMin);
        strWeightAvg = String.format("%.2f", dWeightTotal/iNumber);

        return true;
    }

}